Passed
Push — master ( 1ff847...a1e431 )
by
unknown
02:16
created

CreateContactCommandHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 13 2
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { EmptyContactException } from 'src/Domain/Contact/Exception/EmptyContactException';
5
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
6
import { CreateContactCommand } from './CreateContactCommand';
7
8
@CommandHandler(CreateContactCommand)
9
export class CreateContactCommandHandler {
10
  constructor(
11
    @Inject('IContactRepository')
12
    private readonly contactRepository: IContactRepository
13
  ) {}
14
15
  public async execute(command: CreateContactCommand): Promise<string> {
16
    const { firstName, lastName, company, email, phoneNumber, notes } = command;
17
18
    if (!firstName && !lastName && !company) {
19
      throw new EmptyContactException();
20
    }
21
22
    const contact = await this.contactRepository.save(
23
      new Contact(firstName, lastName, company, email, phoneNumber, notes)
24
    );
25
26
    return contact.getId();
27
  }
28
}
29